package com.gxuwz.prrs.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.activity.*;
import com.gxuwz.prrs.MainActivity;

/**
 * @author 张钧
 * @Description
 * @create 2023-05-24 上午 11:08
 */
public class NavigationUtils {

	public static void redirectToMainActivity(Context context, Bundle bundle) {
		redirect(context, bundle, MainActivity.class);
	}

	public static void redirectToChangePasswordActivity(Context context, Bundle bundle) {
		redirect(context, bundle, ChangePasswordActivity.class);
	}

	public static void redirectToNoticesActivity(Context context, Bundle bundle) {
		redirect(context, bundle, NoticesActivity.class);
	}

	public static void redirectToNoticeDetailActivity(Context context, Bundle bundle) {
		redirect(context, bundle, NoticeDetailActivity.class);
	}

	public static void redirectToLoginActivity(Context context, Bundle bundle) {
		redirect(context, bundle, LoginActivity.class);
	}

	public static void redirectToPianoRoomsActivity(Context context, Bundle bundle) {
		redirect(context, bundle, PianoRoomsActivity.class);
	}

	public static void redirectToAppointmentRecordDetailActivity(Context context, Bundle bundle) {
		redirect(context, bundle, AppointmentRecordDetailActivity.class);
	}

	public static void redirectToAppointmentRecordsActivity(Context context, Bundle bundle) {
		redirect(context, bundle, AppointmentRecordsActivity.class);
	}

	public static void redirectToMineActivity(Context context, Bundle bundle) {
		redirect(context, bundle, MineActivity.class);
	}

	public static void redirect(Context context, Bundle bundle, Class<?> to) {
		if(!context.getClass().equals(to)){
			Intent intent = new Intent(context, to);
			if (bundle == null) {
				bundle = new Bundle();
			}
			bundle.putString("from", context.getClass().getName());
			intent.putExtras(bundle);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		}
	}

	public static void setBack(int back, AppCompatActivity activity) {
		ImageView backImage = activity.findViewById(back);
		Intent intent = activity.getIntent();
		Bundle extras = intent.getExtras();
		String from = extras.getString("from");
		Class<?> clazz;
		try {
			clazz = Class.forName(from);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
		backImage.setOnClickListener(view -> redirect(activity, null, clazz));
	}

	public static void setRedirectBottom(AppCompatActivity activity) {
		ImageView toHome = activity.findViewById(R.id.to_home);
		ImageView toNoticeImage = activity.findViewById(R.id.to_notice);
		ImageView toMine = activity.findViewById(R.id.to_mine);
		toHome.setOnClickListener(view -> redirectToMainActivity(activity, null));
		toNoticeImage.setOnClickListener(view -> redirectToNoticesActivity(activity, null));
		toMine.setOnClickListener(view -> redirectToMineActivity(activity, null));
	}
}
